package com.xunshangwang.pinhaohuo.ui.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.tools.SPHelper;


/**
 * Created by xzp on 15/9/24.
 */
public class WelcomeActivity extends BaseActivity{
    Handler mHandler=new Handler();
    private long mTime;
    private long mLastTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        SharedPreferences.Editor editor=getSharedPreferences(SPHelper.SP_CONFIG,MODE_PRIVATE).edit();
        int startCount=SPHelper.getIntValue(this,SPHelper.SP_CONFIG,SPHelper.APP_START_COUNT);
        if(startCount==0)
        {
            DisplayMetrics metrics=new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            editor.putInt(SPHelper.SCREEN_WIDTH_PIXELS, metrics.widthPixels);
            editor.putInt(SPHelper.SCREEN_HEIGHT_PIXELS,metrics.heightPixels);
        }
        editor.putInt(SPHelper.APP_START_COUNT, startCount++);
        editor.commit();
        mTime=2500;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mLastTime=System.currentTimeMillis();
        mHandler.postDelayed(mRun, mTime);
    }

    @Override
    protected void onPause() {
        mHandler.removeCallbacks(mRun);
        mTime=mTime-(System.currentTimeMillis()-mLastTime);
        super.onPause();
    }

    private Runnable mRun=new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
            overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
            finish();
        }
    };

    @Override
    public void onBackPressed() {
        mHandler.removeCallbacks(mRun);
        super.onBackPressed();
    }
}
